package com.example.back.controller;

import com.example.back.model.Boardgame;
import com.example.back.model.User;
import com.example.back.service.BoardgameService;
import com.example.back.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 桌游控制层
 */
@RestController
@RequestMapping("/boardgame")
public class BoardgameController {
    private static final Logger logger = LogManager.getLogger(BoardgameController.class);

    @Resource
    private BoardgameService boardgameService;

    @RequestMapping("/getBoardgameList")
    @ResponseBody
    public List<Boardgame> getBoardgameList(){
        return boardgameService.findAllBoardgame();
    }

    @RequestMapping("/insertBoardgame")
    public String insertBoardgame(String name, String introduction, int lowerLimit, int upperLimit, int rank) {
        String rs = "";
        if (boardgameService.insertBoardgame(name, introduction, lowerLimit, upperLimit, rank, 0)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>新桌游添加成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>新桌游添加失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/updateBoardgame")
    public String updateBoardgame(int id, int rank, int position) {
        String rs = "";
        if (rank != 0 && position != 0) {
            if (boardgameService.updateRankByID(id, rank) && boardgameService.updatePositionByID(id, position)) {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>桌游排名和位置信息更新成功！点击返回（3秒后自动返回）</button>";
            } else {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>桌游排名和位置信息未完全更新！点击返回（3秒后自动返回）</button>";
            }
        } else if (rank != 0) {
            if (boardgameService.updateRankByID(id, rank)) {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>桌游排名更新成功！点击返回（3秒后自动返回）</button>";
            } else {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>桌游排名更新失败！点击返回（3秒后自动返回）</button>";
            }
        } else if (position != 0) {
            if (boardgameService.updatePositionByID(id, position)) {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>桌游位置信息更新成功！点击返回（3秒后自动返回）</button>";
            } else {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>桌游位置信息更新失败！点击返回（3秒后自动返回）</button>";
            }
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>桌游排名和位置信息输入非法，请重新输入！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/deleteBoardgame")
    public String deleteBoardgame(int id) {
        String rs = "";
        if (boardgameService.deleteBoardgame(id)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除桌游成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除桌游失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("watchBPhoto")
    public String watchBPhoto(int id) {
        return "<img src='" + boardgameService.findPhotoByID(id) + "'>";
    }
}
